using System.IO;
using System.Text;
using UnityEditor;

using UnityEngine;

namespace CGF
{
    public class CreatePrefabComponent
    {
        [MenuItem("Assets/创建 Prefab 脚本")]
        public static void Create()
        {
            GameObject[] gos = Selection.GetFiltered<GameObject>(SelectionMode.Assets);
            for (int i = 0; i < gos.Length; i++)
            {
                CreateComponentWithPrefab(gos[i]);
            }
        }

        public static void CreateComponentWithPrefab(GameObject go)
        {
            string comTemPath = $"{Application.dataPath}/{Define.ScriptsHotfixFolder}/CGFramework/Editor/CreatePrefabComponent/Templates/PrefabComponent.cs.txt";

            string prefabPath = AssetDatabase.GetAssetPath(go);
            if (string.IsNullOrEmpty(prefabPath))
            {
                GameObject prefab = PrefabUtility.GetCorrespondingObjectFromOriginalSource(go);
                prefabPath = AssetDatabase.GetAssetPath(prefab);
            }
            if (string.IsNullOrEmpty(prefabPath))
            {
                UnityEditor.SceneManagement.PrefabStage prefabStage = UnityEditor.SceneManagement.PrefabStageUtility.GetPrefabStage(go);
                if (prefabStage)
                {
                    prefabPath = prefabStage.assetPath;
                }
            }
            string saveFolder = Path.GetDirectoryName(prefabPath).Replace("\\", "/").Replace($"Assets/{Define.BundleFolder}/Prefabs/", "");
            saveFolder = $"{Application.dataPath}/{Define.DevelopScriptDir}/{saveFolder}";
            string comCode = File.ReadAllText(comTemPath);
            comCode = comCode.Replace("#ClassName#", go.name);
            comCode = comCode.Replace("#PrefabPath#", $"{prefabPath.Replace($"Assets/{Define.BundleFolder}/", "").Replace($".prefab", "")}");
            if (go.GetComponent<Reference>())
            {
                comCode = comCode.Replace("#BindProperties#", "\n\t\t\tBindProperties(transform);");
            }
            else
            {
                comCode = comCode.Replace("#BindProperties#", "");
            }
            string comSavePath = $"{saveFolder}/{go.name}.cs";
            Debug.Log($"comSavePath:{comSavePath}");
            if (!File.Exists(comSavePath)) SaveFile(comSavePath, comCode);

            Debug.Log($"生成 {go.name}.cs 脚本完成");

            //创建Partial类
            ReferenceEditor.CreateComponentPartial(go);

            AssetDatabase.Refresh();
        }

        private static void SaveFile(string path, string content)
        {
            string folder = System.IO.Path.GetDirectoryName(path);
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            File.WriteAllText(path, content, new UTF8Encoding(false));
        }

        public static int GetPartialFileState(GameObject go)
        {
            string prefabPath = AssetDatabase.GetAssetPath(go);
            if (string.IsNullOrEmpty(prefabPath))
            {
                GameObject prefab = PrefabUtility.GetCorrespondingObjectFromOriginalSource(go);
                prefabPath = AssetDatabase.GetAssetPath(prefab);
            }
            if (string.IsNullOrEmpty(prefabPath))
            {
                UnityEditor.SceneManagement.PrefabStage prefabStage = UnityEditor.SceneManagement.PrefabStageUtility.GetPrefabStage(go);
                if (prefabStage)
                {
                    prefabPath = prefabStage.assetPath;
                }
            }
            string saveFolder = Path.GetDirectoryName(prefabPath).Replace("\\", "/").Replace($"Assets/{Define.BundleFolder}/Prefabs/", "");
            saveFolder = $"{Application.dataPath}/{Define.ScriptsHotfixFolder}/AutoGenerate/{saveFolder}/{go.name}";
            string partialSavePath = $"{saveFolder}/{go.name}Partial.cs";
            if (File.Exists(partialSavePath))
            {
                //AutoGenerate存在没有Component结尾的Partial类
                return 1;
            }
            partialSavePath = $"{saveFolder}/{go.name}ComponentPartial.cs";
            if (File.Exists(partialSavePath))
            {
                //AutoGenerate存在有Component结尾的Partial类
                return 2;
            }
            return 0;
        }
    }
}